/*
 * jxDao Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxDao.dao.db;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.jxUtils.check.ParameterCheck;
import org.jxUtils.file.JXFile;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxDao
 * @package org.jxDao.dao.db
 * @date 11/04/2014 - 13:46:31
 */
public class DBInfo extends Properties {
	
	private static final long serialVersionUID = 2132757784051291765L;
	
	private static final String HOST = "host";
	
	private static final String PORT = "port";
	
	private static final String DATABASE = "database";
	
	private static final String PASSWORD = "password";
	
	private static final String USER = "user";
	
	private DBMS dbms;
	
	/**
	 * @param driver
	 */
	public DBInfo(final Driver driver) {
	
		this("", "", "", "", driver);
	}
	
	/**
	 * To ODBC
	 * 
	 * @param database
	 */
	public DBInfo(final String database) {
		
		ParameterCheck.isNullOrAbsoluteEmpty(database, "database");
		
		init("", "", database, "", "", Driver.ODBC);
	}
	
	/**
	 * @param host
	 * @param port
	 * @param database
	 * @param user
	 * @param driver
	 */
	public DBInfo(final String host, final String port, final String database, final String user, final Driver driver) {
	
		this(host, port, database, user, "", driver);
	}
	
	/**
	 * @param host
	 * @param port
	 * @param database
	 * @param user
	 * @param password
	 * @param driver
	 */
	public DBInfo(final String host, final String port, final String database, final String user, final String password, final Driver driver) {
		
		init(host, port, database, user, password, driver);
	}
	
	/**
	 * @param <T>
	 * @param file
	 * @return object
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public static final <T extends DBInfo> T loadObject(final File file) throws IOException, ClassNotFoundException {
	
		return JXFile.readObject(file);
	}
	
	/**
	 * @param host
	 * @param port
	 * @param database
	 * @param user
	 * @param password
	 * @param driver
	 */
	private void init(final String host, final String port, final String database, final String user, final String password, final Driver driver) {
		
		ParameterCheck.isNull(driver, "driver");
		
		setHost(host);
		setPort(port);
		setDatabase(database);
		setUser(user);
		setPassword(password);
		setDBMS(DBMS.getDBMS(driver, this));
	}
	
	/**
	 * @param dbms
	 */
	final void setDBMS(final DBMS dbms) {
	
		ParameterCheck.isNull(dbms, "dbms");
		
		this.dbms = dbms;
	}
	
	/**
	 * @return database
	 */
	public final String getDatabase() {
	
		return (String)get(DATABASE);
	}
	
	/**
	 * @return dbms
	 */
	public final DBMS getDBMS() {
	
		return dbms;
	}
	
	/**
	 * @return driver
	 */
	public final Driver getDriver() {
	
		return getDBMS().getDriver();
	}
	
	/**
	 * @return host
	 */
	public final String getHost() {
	
		return (String)get(HOST);
	}
	
	/**
	 * @return password
	 */
	public final String getPassword() {
	
		return (String)get(PASSWORD);
	}
	
	/**
	 * @return port
	 */
	public final String getPort() {
	
		return (String)get(PORT);
	}
	
	/**
	 * @return url
	 */
	public final String getUrl() {
	
		return getDBMS().getUrl();
	}
	
	/**
	 * @return user
	 */
	public final String getUser() {
	
		return (String)get(USER);
	}
	
	/**
	 * @param file
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public final void load(final File file) throws FileNotFoundException, IOException {
	
		load(new FileInputStream(file));
	}
	
	/**
	 * @param file
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public final void save(final File file) throws FileNotFoundException, IOException {
	
		save(file, "");
	}
	
	/**
	 * @param file
	 * @param comments
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public final void save(final File file, final String comments) throws FileNotFoundException, IOException {
	
		store(new FileOutputStream(file), comments);
	}
	
	/**
	 * @param object
	 * @throws IOException
	 */
	public final void saveObject(final File file) throws IOException {
	
		JXFile.writeObject(this, file, false);
	}
	
	/**
	 * @param object
	 * @param hidden
	 * @throws IOException
	 */
	public final void saveObject(final File file, final boolean hidden) throws IOException {
	
		JXFile.writeObject(this, file, hidden);
	}
	
	/**
	 * @param database
	 */
	public final void setDatabase(final String database) {
	
		put(DATABASE, database != null ? database.trim() : "");
	}
	
	/**
	 * @param host
	 */
	public final void setHost(final String host) {
	
		put(HOST, host != null ? host.trim() : "");
	}
	
	/**
	 * @param password
	 */
	public final void setPassword(final String password) {
	
		put(PASSWORD, password != null ? password.trim() : "");
	}
	
	/**
	 * @param port
	 */
	public final void setPort(final String port) {
	
		put(PORT, port != null ? port.trim() : "");
	}
	
	/**
	 * @param user
	 */
	public final void setUser(final String user) {
	
		put(USER, user != null ? user.trim() : "");
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.Hashtable#toString()
	 */
	@Override
	public String toString() {
	
		return "[" + getDBMS() + "] " + getHost() + ":" + getPort() + " - " + getDatabase();
	}
}
